#include "superbigiconitem.h"
#include "ui_superbigiconitem.h"

#include <QFontMetrics>
#include <QToolButton>

SuperBigIconItem::SuperBigIconItem(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SuperBigIconItem)
{
    ui->setupUi(this);
    initMark();
}

SuperBigIconItem::~SuperBigIconItem()
{
    delete ui;
}

void SuperBigIconItem::setImage()
{
    // 可以修改为当模式为模版时, 读取模版图片, 否则采用默认图片
    QPixmap pixmap1(":/action/images/model.png");
    pixmap1 = pixmap1.scaled(ui->labelImage->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    ui->labelImage->setPixmap(pixmap1);

    // 公司logo
    QPixmap pixmap2(":/action/images/companyLogo.png");
    pixmap2 = pixmap2.scaled(ui->labelHead->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    ui->labelHead->setPixmap(pixmap2);
}

void SuperBigIconItem::setText(const QString &libName, const QString DPI, const QString description)
{
    ui->labelLibName->setText(libName);
    ui->labelDPI->setText(DPI);

    // 设置文本过长时截断
    QFontMetrics metrics(ui->labelDescription->font());
    QString elidedText = metrics.elidedText(description, Qt::ElideRight,
                                            ui->labelDescription->width());

    ui->labelDescription->setText(elidedText);
}

void SuperBigIconItem::initMark()
{
    widget = new QWidget(this);

    widget->setFixedSize(380, 225);
    widget->move(0, 0);
    widget->setStyleSheet("background-color: rgba(0, 0, 0, 180);");

    // 添加两个按钮
    QToolButton *buttonEdit = new QToolButton(widget);
    QToolButton *buttonDelete = new QToolButton(widget);

    QIcon iconEdit(":/action/images/edit_white.png");
    QIcon iconDelete(":/action/images/delete_white.png");

    buttonEdit->setIcon(iconEdit);
    buttonDelete->setIcon(iconDelete);

    buttonEdit->setFixedSize(60, 60);
    buttonDelete->setFixedSize(60, 60);

    buttonEdit->setIconSize(QSize(40, 40));
    buttonDelete->setIconSize(QSize(40, 40));

    buttonEdit->setText("编辑");
    buttonDelete->setText("删除");

    QPalette palette = buttonEdit->palette();
    palette.setColor(QPalette::ButtonText, Qt::white);
    buttonEdit->setPalette(palette);
    buttonDelete->setPalette(palette);

    QFont font = buttonEdit->font();
    font.setFamily("微软雅黑");
    buttonEdit->setFont(font);
    buttonDelete->setFont(font);

    buttonEdit->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    buttonDelete->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

    buttonEdit->setStyleSheet("QToolButton {"
                              "    background: transparent;"
                              "    border: none;"
                              "}");
    buttonDelete->setStyleSheet("QToolButton {"
                              "    background: transparent;"
                              "    border: none;"
                              "}");

    buttonEdit->setGeometry(100, 85, 60, 60);
    buttonDelete->setGeometry(220, 85, 60, 60);


    widget->setVisible(false);
}

void SuperBigIconItem::enterEvent(QEvent *event)
{
    widget->setVisible(true);

    QWidget::enterEvent(event);
}

void SuperBigIconItem::leaveEvent(QEvent *event)
{
    widget->setVisible(false);

    QWidget::enterEvent(event);
}
